<div class="wrapper">
<div class="main">
	<div class="msg-container">
		<!-- <div class="msg-list"></div> -->
	</div>
<div class="input-board">
	<form action="javascript:void(0)" method="get" id="f" role="form">
		<div class="form-group">
			<!-- <textarea class="form-control" rows="3" name="text" id="text"></textarea> -->
			<input type="text" class="form-control" name="text" id="text">
		</div>
		<input type="hidden" name="type" value="group">
		<input type="hidden" name="toId" value="1">
		<button type="submit" class="btn btn-info" onclick="s()">send</button>
		<a href="<?php echo site_url('login/logout');?>" class="btn btn-danger">exit</a>
	</form>
</div>
</div>
</div>
<script>
	var qReq = null, sReq = null;
	window.onload = function() {
		window.onbeforeunload = function() {

		}
	}
	q();
	function q() {
		if (qReq != null) {
			qReq.abort();
		}
		qReq = $.ajax({
			type: 'POST',
			url:  "<?php echo site_url('chat/calc');?>",
			async: true,
			success: function (data, textStatus) {
				var json = eval('(' + data + ')');
				for (var j in json) {
					var ap = '<div class="text-info msg msg-q msg-new">';
					ap += '<span class="msg-head">';
					ap += json[j].fromName + '(' + json[j].fromId + ')';
					ap += ' ' + json[j].time;
					ap += '</span><br>';
					ap += '<span class="msg-body"></span>';
					// ap += json[j].text.replace(/(\[removed\])/g, '');
					ap += '</div>';
					$('.msg-container').append(ap);
					$('.msg-q.msg-new>.msg-body').text(json[j].text);
					$('.msg-q.msg-new').toggleClass('msg-new');
					$('html, body').animate({scrollTop:$(document).height()}, 'slow');
				}
				if (json.length > 0)
					qc(data);
				setTimeout('q()', 0);
			},
			error: function() {
				setTimeout('q()', 0);
			}
		});
	}
	function s() {
		if (sReq != null) {
			sReq.abort();
		}
		sReq = $.ajax({
			type: 'POST',
			url:  "<?php echo site_url('chat/rec');?>",
			data: $('#f').serialize(),
			async: true,
			success: function (data, textStatus) {
				var json = eval('(' + data + ')');
				if(json.status) {
					var ap = '<div class="text-warning msg msg-s msg-new">';
					// var text = json.text.replace(/(\[removed\])/g, '');
					var reg = new RegExp('\n', 'g');
					ap += '<span class="msg-head">';
					ap += "<?php echo $name . '(' . $username . ')';?>";
					ap += ' ' + json.time;
					ap +='</span><br>';
					ap += '<span class="msg-body"></span>';
					ap += '</div>';
					$('#text').val('');
					$('.msg-container').append(ap);
					$('.msg-s.msg-new>.msg-body').text(json.text);
					$('.msg-s.msg-new').toggleClass('msg-new');
					$('html, body').animate({scrollTop:$(document).height()}, 'slow');
				}
			},
			error: function() {
				setTimeout('s()', 0);
			}
		});
	}
	function qc(json) {
		$.ajax({
			type: 'POST',
			url:  "<?php echo site_url('chat/conf');?>",
			data: {
				'json': json
			},
			async: false,
			success: function (data, textStatus) {
				console.log(data);
			}
		});
	}
</script>